<template>
  <div class="song-play-i" :style="'width:'+width+';height:'+height">
    <div class="item-play">
      <ImgLazy
        :url="require(`@/assets/images/home/btn-play.png`)"
        @mouseenter="MouseEvent"
        @mouseleave="MouseLeave"
      />
      <transition :name="isEnter?'playMouse':''">
        <div class="play-animation" v-if="isEnter"></div>
      </transition>
    </div>
  </div>
</template>

<script>
import {reactive,toRefs,onBeforeUnmount} from 'vue'
export default {
    name:"PlayButton",
    props: {
        duration:{
            type:Number,
            default:1000
        },
        width:{
            type:String,
            default:"50px"
        },
        height:{
            type:String,
            default:"50px"
        }
    },
    setup(props){
        const state = reactive({
            isEnter:false,
        })

        let timer = null;
        const isPlay = (isE) =>{
            if(isE){
                state.isEnter=true;
                timer=setInterval(()=>{
                    state.isEnter = !state.isEnter;
                },props.duration)
            }
        }

        const MouseEvent = ()=>{
            isPlay(true)
        }
        
        const MouseLeave = ()=>{
            state.isEnter=false;
            _clearIntervalFn();
        }

        onBeforeUnmount(()=>{ //组件卸载
            _clearIntervalFn();
        })
        
        const _clearIntervalFn = ()=>{
            clearInterval(timer); // 清除计时器
            timer = null;
        }
        return{
            ...toRefs(state),
            MouseEvent,MouseLeave
        }
    }
}
</script>

<style lang="scss" scoped>
.song-play-i {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  cursor: pointer;
  border-radius: 50%;
  z-index: 3;

  img {
    width: 100%;
  }
  .item-play {
    width: 100%;
    height: 100%;
    position: relative;

    .play-animation {
      width: 100%;
      height: 100%;
      position: absolute;
      z-index: -1;
      border-radius: 50%;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

    img {
      width: 100%;
    }
  }
}

.playMouse-enter-active {
  animation: PMouse 0.8s;
}
.playMouse-leave-active {
  animation: PMouse 0.8s;
}

@keyframes PMouse {
  0% {
    border: 1px solid $ThemeColor;
    opacity: 0.7;
  }
  100% {
    border: 2rem solid $ThemeColor;
    opacity: 0;
  }
}
</style>